<template>
    <el-container class="topic">
        <el-header class="topic__header">
            <div class="topic__header-main">
                <el-button type="primary" size="small" @click="openDialog('add')">新增主题</el-button>
            </div>
        </el-header>
        <el-main class="topic__content">
            <div ref="container" class="table-container">
                <el-table ref="table" :data="topics" style="width: 100%" align="center" header-align="center">
                    <el-table-column label="主题名称" prop="title" />

                 
                    <el-table-column label="操作" >
                        <template slot-scope="scope">
                            <el-button size="small" type="primary" @click="toTeacher(scope.row)">讲师</el-button>
                            <el-button size="small" type="warning" @click="toMaterial(scope.row)">材料</el-button>
                            <el-button size="small" type="success" @click="toLesson(scope.row)">课程</el-button>
                        </template>
                    </el-table-column>

                    
                    <el-table-column label="主题操作" >
                        <template slot-scope="scope">
                            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除主题</el-button>
                            <el-button size="small" type="info" @click="openDialog('edit', scope.row)">修改主题名称</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </el-main>

        <dialog-edit :visible.sync="dialogVisible" v-if="dialogVisible" @refresh="refresh" ref="edit"></dialog-edit>
    </el-container>
</template>

<script>
import dialogEdit from "./components/edit.vue"

export default {
    name: "topicList",
    components: {
        dialogEdit
    },
    data() {
        return {
            topics: [],
            dialogVisible: false,
        };
    },

    created() {
        this.refresh();
    },
    methods: {
        refresh() {
            this.fetchTopics();
        },
        async fetchTopics() {
            try {
                const { data } = await this.$fetch.get("/api/topic/list");
                this.topics = data;
            } catch (error) {
                console.log(error);
            }
        },

        async handleDelete(item) {
            try {
                const { code } = await this.$fetch.post("/api/topic/delete", {
                    id: item.id
                });
                if (code === 0) {
                    this.$message.success("删除成功");
                    this.refresh();
                }
            } catch (error) {
                this.$message.error(error.message);
            } 
        },

        openDialog(type, row) {
            this.dialogVisible = true;
            this.$nextTick(() => {
                this.$refs.edit.init(type, row);
            })
        },
        toTeacher(item) {
            this.$router.push({
                path: "/topic/teacher",
                query: {
                    topicId: item.id,
                    title: item.title,
                },
            });
        },

        toMaterial(item) {
            this.$router.push({
                path: "/topic/material",
                query: {
                    topicId: item.id,
                    title: item.title,
                },
            });
        },

        toLesson(item) {
            this.$router.push({
                path: "/topic/lesson",
                query: {
                    topicId: item.id,
                    title: item.title,
                },
            });
        },

        delete(row) {
            const params = {
                id: row.id,
            };
            this.$confirm("确定要删除此主题嘛？", "", {
                distinguishCancelAndClose: true,
                confirmButtonText: "确认",
                cancelButtonText: "取消",
            }).then(async () => {
                const { code } = await this.$fetch.post("/api/user/delete", params);
                if (code === 0) {
                    this.$message.success("删除主题成功");
                }
                this.refresh();
            });
        },
    },
};
</script>

<style scoped lang="scss">
.topic {
    display: flex;
    flex-direction: column;
    height: calc(100vh - 50px);

    .topic__header {
        display: flex;
        align-items: center;

        .topic__header-main {
            height: 40px;
            display: flex;
            align-items: center;

            .management__headerInput {
                margin-right: 20px;
            }
        }
    }

    .topic__content {
        display: flex;
        flex-direction: column;
        flex: 1;
        overflow: auto;
        padding-bottom: 0;

        .table-container {
            flex: 1;
            overflow: hidden;
            overflow-y: auto;
        }
    }

    .pagination-container {
        display: flex;
        justify-content: center;
        padding: 20px 0;

        .el-pagination {
            padding: 20px 0;
        }
    }
}
</style>
